<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from /home/jaffer/scheme/r5rs.txi on 28 March 2001 -->

<TITLE>Revised(5) Scheme - Example</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="r5rs_1.html">first</A>, <A HREF="r5rs_11.html">previous</A>, <A HREF="r5rs_13.html">next</A>, <A HREF="r5rs_14.html">last</A> section, <A HREF="r5rs_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC84" HREF="r5rs_toc.html#TOC84">Example</A></H1>
<P>
 


<P>
<A NAME="IDX496"></A>


<P>
<SAMP>`Integrate-system'</SAMP> integrates the system 


<P>
y_k^^ = f_k(y_1, y_2, ..., y_n),    k = 1, ..., n


<P>
of differential equations with the method of Runge-Kutta.


<P>
The parameter <TT>system-derivative</TT> is a function that takes a system
state (a vector of values for the state variables y_1, ..., y_n)
and produces a system derivative (the values y_1^^, ...,y_n^^).  The parameter <TT>initial-state</TT> provides an initial
system state, and <TT>h</TT> is an initial guess for the length of the
integration step.


<P>
The value returned by <SAMP>`integrate-system'</SAMP> is an infinite stream of
system states.



<PRE>

(define integrate-system
  (lambda (system-derivative initial-state h)
    (let ((next (runge-kutta-4 system-derivative h)))
      (letrec ((states
                (cons initial-state
                      (delay (map-streams next
                                          states)))))
        states))))

</PRE>

<P>
<SAMP>`Runge-Kutta-4'</SAMP> takes a function, <TT>f</TT>, that produces a
system derivative from a system state.  <SAMP>`Runge-Kutta-4'</SAMP>
produces a function that takes a system state and
produces a new system state.



<PRE>

(define runge-kutta-4
  (lambda (f h)
    (let ((*h (scale-vector h))
          (*2 (scale-vector 2))
          (*1/2 (scale-vector (/ 1 2)))
          (*1/6 (scale-vector (/ 1 6))))
      (lambda (y)
        ;; y is a system state
        (let* ((k0 (*h (f y)))
               (k1 (*h (f (add-vectors y (*1/2 k0)))))
               (k2 (*h (f (add-vectors y (*1/2 k1)))))
               (k3 (*h (f (add-vectors y k2)))))
          (add-vectors y
            (*1/6 (add-vectors k0
                               (*2 k1)
                               (*2 k2)
                               k3))))))))

(define elementwise
  (lambda (f)
    (lambda vectors
      (generate-vector
        (vector-length (car vectors))
        (lambda (i)
          (apply f
                 (map (lambda (v) (vector-ref  v i))
                      vectors)))))))

(define generate-vector
  (lambda (size proc)
    (let ((ans (make-vector size)))
      (letrec ((loop
                (lambda (i)
                  (cond ((= i size) ans)
                        (else
                         (vector-set! ans i (proc i))
                         (loop (+ i 1)))))))
        (loop 0)))))

(define add-vectors (elementwise +))

(define scale-vector
  (lambda (s)
    (elementwise (lambda (x) (* x s)))))

</PRE>

<P>
<SAMP>`Map-streams'</SAMP> is analogous to <SAMP>`map'</SAMP>: it applies its first
argument (a procedure) to all the elements of its second argument (a
stream).



<PRE>

(define map-streams
  (lambda (f s)
    (cons (f (head s))
          (delay (map-streams f (tail s))))))

</PRE>

<P>
Infinite streams are implemented as pairs whose car holds the first
element of the stream and whose cdr holds a promise to deliver the rest
of the stream.



<PRE>

(define head car)
(define tail
  (lambda (stream) (force (cdr stream))))

</PRE>

<P>
The following illustrates the use of <SAMP>`integrate-system'</SAMP> in
integrating the system


<P>
C dv_C / dt = -i_L - v_C / R


<P>
L di_L / dt = v_C


<P>
which models a damped oscillator.



<PRE>

(define damped-oscillator
  (lambda (R L C)
    (lambda (state)
      (let ((Vc (vector-ref state 0))
            (Il (vector-ref state 1)))
        (vector (- 0 (+ (/ Vc (* R C)) (/ Il C)))
                (/ Vc L))))))

(define the-states
  (integrate-system
     (damped-oscillator 10000 1000 .001)
     '#(1 0)
     .01))

</PRE>

<P>
         


<P><HR><P>
Go to the <A HREF="r5rs_1.html">first</A>, <A HREF="r5rs_11.html">previous</A>, <A HREF="r5rs_13.html">next</A>, <A HREF="r5rs_14.html">last</A> section, <A HREF="r5rs_toc.html">table of contents</A>.
</BODY>
</HTML>
